﻿using System.Threading.Tasks;

namespace Sidvall.Security
{
    public class UserResponseStrategy : IUserResponseStrategy
    {
        #region Public Members

        public string CurrentUserResponse { get; set; }
        public bool UseFile { get; set; }
        #region FilePath

        protected virtual string FilePath
        {
            get
            {
                return "userresponse.json";
            }
        }

        #endregion

        #region LoadUserResponseAsync

        public async Task LoadUserResponseAsync()
        {
            string userResponse;

            userResponse = null;
            if ((this.UseFile) && (Sidvall.SystemContext.Current.UserStorageManager != null))
            {
                try
                {
                    if (await Sidvall.SystemContext.Current.UserStorageManager.FileExistsAsync(this.FilePath).ConfigureAwait(false))
                        userResponse = await Sidvall.SystemContext.Current.UserStorageManager.ReadAllTextAsync(this.FilePath).ConfigureAwait(false);
                }
                catch (System.Exception)
                {
                    userResponse = null;
                }
                if (userResponse == null)
                    userResponse = string.Empty;
            }
            this.CurrentUserResponse = userResponse;
        }
        public async Task LoadUserResponseAsync(string userResponse, bool storeInFile)
        {
            await LoadUserResponseAsync(userResponse, (bool?)storeInFile).ConfigureAwait(false);
        }
        private async Task LoadUserResponseAsync(string userResponse, bool? storeInFile)
        {
            this.CurrentUserResponse = null;
            if ((this.UseFile) && (!string.IsNullOrWhiteSpace(this.FilePath)) && (Sidvall.SystemContext.Current.UserStorageManager != null))
            {
                if (userResponse == null)
                    userResponse = string.Empty;
                if (storeInFile == true)
                {
                    this.CurrentUserResponse = userResponse;
                    await Sidvall.SystemContext.Current.UserStorageManager.WriteAllTextAsync(this.FilePath, userResponse).ConfigureAwait(false);
                }
                else if (storeInFile == false)
                {
                    if (await Sidvall.SystemContext.Current.UserStorageManager.FileExistsAsync(this.FilePath).ConfigureAwait(false))
                        await Sidvall.SystemContext.Current.UserStorageManager.DeleteFileAsync(this.FilePath).ConfigureAwait(false);
                }
                else
                {
                    storeInFile = await Sidvall.SystemContext.Current.UserStorageManager.FileExistsAsync(this.FilePath).ConfigureAwait(false);
                    if (storeInFile == true)
                    {
                        this.CurrentUserResponse = userResponse;
                        await Sidvall.SystemContext.Current.UserStorageManager.WriteAllTextAsync(this.FilePath, userResponse).ConfigureAwait(false);
                    }
                }
            }
        }

        #endregion

        #endregion
        #region Constructors

        public UserResponseStrategy() : this(true)
        {
        }
        public UserResponseStrategy(bool useFile)
        {
            this.UseFile = useFile;
        }

        #endregion
    }
}
